package com.dengyuanke.weathercollectionserver.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Author: dyk
 * Create time: 2018/9/19  22:59
 */
@Service
public class WeatherDataColletcionServiceImpl implements WeatherDataCollectionService{

    private final static Logger LOGGER= LoggerFactory.getLogger(WeatherDataColletcionServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //缓存超时时间
    private final Long TIME_OUT=1800L;


    private final String WEATHER_API = "http://wthrcdn.etouch.cn/weather_mini";

    @Override
    public void syncDataByCityId(String cityId) {
        LOGGER.info("Start 同步天气.cityId:"+cityId);

        String uri = WEATHER_API + "?citykey=" + cityId;
        this.saveWeatherData(uri);

        LOGGER.info("End 同步天气");
    }

    private void saveWeatherData(String uri) {
        ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
        String key = uri;
        String strBody = null;

        ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);

        if (response.getStatusCodeValue() == 200) {
            strBody = response.getBody();
        }

        ops.set(key, strBody, TIME_OUT, TimeUnit.SECONDS);
    }

}